# Replication package for 
# "The Economic Leverage of International Organizations in Interstate Disputes"
# Johannes Karreth
# June 30, 2017
# jkarreth@ursinus.edu

# This file: 30_claims-crises_coefficients.R
# Purpose: Create density plots of logit coefficients for IGO coefficients across all models

rm(list = ls())

# setwd("...")

library("ggplot2")
library("dplyr")

# Source in functions
source("Functions/MCMClogit.fd.mat.R")
source("Functions/theme_jk.R")
source("Functions/plotBMA.R")
source("Functions/MCMC_roc_prc.r")
source("Functions/geom_flat_violin.R")

# Claims

m1_stanglm <- readRDS("Output_MCMC/claims_m1_stanglm.RDS")
m2_stanglm <- readRDS("Output_MCMC/claims_m2_stanglm.RDS")
m3_stanglm <- readRDS("Output_MCMC/claims_m3_stanglm.RDS")
m4_stanglm <- readRDS("Output_MCMC/claims_m4_stanglm.RDS")
m5_stanglm <- readRDS("Output_MCMC/claims_m5_stanglm.RDS")
m6_stanglm <- readRDS("Output_MCMC/claims_m6_stanglm.RDS")
m7_stanglm <- readRDS("Output_MCMC/claims_m7_stanglm.RDS")
m8_stanglm <- readRDS("Output_MCMC/claims_m8_stanglm.RDS")
m9_stanglm <- readRDS("Output_MCMC/claims_m9_stanglm.RDS")
m10_stanglm <- readRDS("Output_MCMC/claims_m10_stanglm.RDS")
m14_stanglm <- readRDS("Output_MCMC/claims_m14_stanglm.RDS")

m1 <- data.frame(coef = as.data.frame(m1_stanglm)[, "igo_lev3_count_use_bc"], 
                 Model = "1 (Main model)")
m2 <- data.frame(coef = as.data.frame(m2_stanglm)[, "igo_lev3_count_use_bc"], 
                 Model = "2 (Controlling for structured IGOs)")
m3 <- data.frame(coef = as.data.frame(m3_stanglm)[, "igo_lev3_count_use_bc"], 
                 Model = "3 (Controlling for Highly structured Security IGOs)")
m4 <- data.frame(coef = as.data.frame(m4_stanglm)[, "igo_lev3_count_use_bc"], 
                 Model = "4 (Controlling for Peace-brokering IGOs)")
m5 <- data.frame(coef = as.data.frame(m5_stanglm)[, "igo_lev3_count_use_bc"], 
                 Model = "5 (Controlling for joint memberships in all other IGOs)")
m6 <- data.frame(coef = as.data.frame(m6_stanglm)[, "igo_lev3_count_use_bc"], 
                 Model = "6 (Controlling for the difference in military power)")
m7 <- data.frame(coef = as.data.frame(m7_stanglm)[, "igo_lev3_count_use_bc"], 
                 Model = "7 (Controlling for trade dependence)")
m8 <- data.frame(coef = as.data.frame(m8_stanglm)[, "igo_lev3_count_use_bc"], 
                 Model = "8 (Controlling for economic development)")
m9 <- data.frame(coef = as.data.frame(m9_stanglm)[, "igo_lev3_count_use_bc"], 
                 Model = "9 (Indicators for regions included)")
m10 <- data.frame(coef = as.data.frame(m10_stanglm)[, "igo_lev3_count_use_bc"], 
                  Model = "10 (Indicator for Cold War era included)")
m11 <- data.frame(coef = as.data.frame(m14_stanglm)[, "igo_lev3_count_use_bc"], 
                  Model = "11 (Aggregate indicator for claim salience used)")

df <- rbind(m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11)

p <- ggplot(data = df, aes(x = coef, group = Model, color = Model, fill = Model)) + 
      geom_vline(xintercept = 0) +
      geom_density(alpha = 0.2) +
      theme_jk() + 
      scale_y_continuous(breaks = NULL) + 
      ylab("") + 
      xlab("Estimate for logit coefficient on\njoint memberships in IGOs with high leverage")

ggsave(p, file = "Output_Tables-and-Figures/claims_igo_coefficients.pdf", width = 8, height = 4)

# Crises

rm(list = ls())

# setwd("...")

library("ggplot2")
library("dplyr")

# Source in functions
source("Functions/MCMClogit.fd.mat.R")
source("Functions/theme_jk.R")
source("Functions/plotBMA.R")
source("Functions/MCMC_roc_prc.r")
source("Functions/geom_flat_violin.R")

m1_stanglm <- readRDS("Output_MCMC/crises_m1_stanglm.RDS")
m2_stanglm <- readRDS("Output_MCMC/crises_m2_stanglm.RDS")
m3_stanglm <- readRDS("Output_MCMC/crises_m3_stanglm.RDS")
m4_stanglm <- readRDS("Output_MCMC/crises_m4_stanglm.RDS")
m5_stanglm <- readRDS("Output_MCMC/crises_m5_stanglm.RDS")
m6_stanglm <- readRDS("Output_MCMC/crises_m6_stanglm.RDS")
m7_stanglm <- readRDS("Output_MCMC/crises_m7_stanglm.RDS")
m8_stanglm <- readRDS("Output_MCMC/crises_m8_stanglm.RDS")
m9_stanglm <- readRDS("Output_MCMC/crises_m9_stanglm.RDS")
m10_stanglm <- readRDS("Output_MCMC/crises_m10_stanglm.RDS")

m1 <- data.frame(coef = as.data.frame(m1_stanglm)[, "igo_lev3_count_use"], 
                 Model = "1 (Main model)")
m2 <- data.frame(coef = as.data.frame(m2_stanglm)[, "igo_lev3_count_use"], 
                 Model = "2 (Controlling for structured IGOs)")
m3 <- data.frame(coef = as.data.frame(m3_stanglm)[, "igo_lev3_count_use"], 
                 Model = "3 (Controlling for Highly structured Security IGOs)")
m4 <- data.frame(coef = as.data.frame(m4_stanglm)[, "igo_lev3_count_use"], 
                 Model = "4 (Controlling for Peace-brokering IGOs)")
m5 <- data.frame(coef = as.data.frame(m5_stanglm)[, "igo_lev3_count_use"], 
                 Model = "5 (Controlling for joint memberships in all other IGOs)")
m6 <- data.frame(coef = as.data.frame(m6_stanglm)[, "igo_lev3_count_use"], 
                 Model = "6 (Controlling for the difference in military power)")
m7 <- data.frame(coef = as.data.frame(m7_stanglm)[, "igo_lev3_count_use"], 
                 Model = "7 (Controlling for trade dependence)")
m8 <- data.frame(coef = as.data.frame(m8_stanglm)[, "igo_lev3_count_use"], 
                 Model = "8 (Controlling for economic development)")
m9 <- data.frame(coef = as.data.frame(m9_stanglm)[, "igo_lev3_count_use"], 
                 Model = "9 (Indicators for regions included)")
m10 <- data.frame(coef = as.data.frame(m10_stanglm)[, "igo_lev3_count_use"], 
                  Model = "10 (Indicator for Cold War era included)")

df <- rbind(m1, m2, m3, m4, m5, m6, m7, m8, m9, m10)

p <- ggplot(data = df, aes(x = coef, group = Model, color = Model, fill = Model)) + 
  geom_vline(xintercept = 0) +
  geom_density(alpha = 0.2) +
  theme_jk() + 
  scale_y_continuous(breaks = NULL) + 
  ylab("") + 
  xlab("Estimate for logit coefficient on\njoint memberships in IGOs with high leverage")

ggsave(p, file = "Output_Tables-and-Figures/crises_igo_coefficients.pdf", width = 8, height = 4)